package com.heer.gymnasium.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.heer.api.ResponseData.ApiResponse;
import com.heer.api.callback.BaseCallback;
import com.heer.gymnasium.R;
import com.heer.gymnasium.adapter.RechargeOfferAdapter;
import com.heer.gymnasium.constant.Extras;
import com.heer.gymnasium.util.PayUtils;
import com.heer.model.CreateRechargeBean;
import com.heer.model.OrderBean;
import com.heer.model.SiteOfferBean;
import com.heer.model.WxPayBean;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by heer on 17/4/23.
 */

public class OrderSuppleActivity extends BaseActivity {
    @BindView(R.id.radio_1)
    RadioButton radio1;
    @BindView(R.id.radio_2)
    RadioButton radio2;
    @BindView(R.id.radio_3)
    RadioButton radio3;
    @BindView(R.id.radio_group)
    RadioGroup radioGroup;
    @BindView(R.id.edit_other)
    EditText editOther;
    @BindView(R.id.btn_submit)
    Button btnSubmit;

    private PayUtils payUtils;

    public static void toOrderSupply(Context context, String sid) {
        Intent intent = new Intent();
        intent.setClass(context, OrderSuppleActivity.class);
        intent.putExtra(Extras.EXTRA_ID, sid);
        context.startActivity(intent);
    }

    @Override
    protected void init() {
        super.init();
        setContentView(R.layout.activity_order_supple);
        payUtils = new PayUtils(this);
    }

    @Override
    protected void initView() {
        super.initView();
        titleBuilder.setTitleText("补交费用");
    }

    @Override
    protected void loadData() {
        super.loadData();

    }

    @OnClick(R.id.btn_submit)
    public void onClick(final View v) {
        final String payWay;

        String money = "0.01";
        money = editOther.getText().toString();
        String regx = "[1-9]\\d*(\\.\\d+)?";
        Pattern p = Pattern.compile(regx);
        Matcher m = p.matcher(money);
        if(!m.find()){
            Toast.makeText(mContext, "请输入补交金额", Toast.LENGTH_SHORT).show();
            return;
        }

        switch (radioGroup.getCheckedRadioButtonId()) {
            case R.id.radio_1:
                payWay = "ali";
                break;
            case R.id.radio_2:
                payWay = "wx";
                break;
            case R.id.radio_3:
                payWay = "bal";
                break;
            default:
                payWay = "bal";
                break;
        }
        Toast.makeText(mContext, "提交", Toast.LENGTH_SHORT).show();
        final String finalMoney = money;
        mAppHelper.apiService().createSupplement(getIntent().getStringExtra(Extras.EXTRA_ID), payWay, money).enqueue(new BaseCallback<ApiResponse<OrderBean>>() {
            @Override
            public void onResponse(Call<ApiResponse<OrderBean>> call, Response<ApiResponse<OrderBean>> response) {
                super.onResponse(call, response);
                if (payWay.equals("ali")) {
                    payUtils.pay(v, finalMoney, response.body().getData().getOrder_sn());
                } else {
                    mAppHelper.apiService().getWxData(response.body().getData().getOrder_sn()).enqueue(new BaseCallback<ApiResponse<WxPayBean>>() {
                        @Override
                        public void onResponse(Call<ApiResponse<WxPayBean>> call, Response<ApiResponse<WxPayBean>> response) {
                            super.onResponse(call, response);
                            if (response.isSuccessful() && response.body().isSccessful()) {
                                WxPayBean data = response.body().getData();
                                Gson gson = new Gson();
                                String json = gson.toJson(data);
                                payUtils.payWx(json);
                            } else {
                                Log.e("PAY_GET", "返回错误" + response.message());
                            }
                        }

                        @Override
                        public void onFailure(Call<ApiResponse<WxPayBean>> call, Throwable t) {
                            super.onFailure(call, t);
                        }
                    });
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<OrderBean>> call, Throwable t) {
                super.onFailure(call, t);
            }
        });
    }
}
